package com.buu.system.controller;

import com.buu.common.annotation.Log;
import com.buu.common.utils.MD5Utils;
import com.buu.common.utils.PageUtils;
import com.buu.common.utils.Query;
import com.buu.common.utils.R;
import com.buu.system.domain.RoleDO;
import com.buu.system.domain.SysUserDO;
import com.buu.system.service.RoleService;
import com.buu.system.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/sys/user")
@Controller
public class UserController {
	@Autowired
	UserService userService;
	@Autowired
	RoleService roleService;

	@GetMapping("")
	String user(Model model) {
		return "sys/user/user";
	}

	@GetMapping("/list")
	@ResponseBody
	PageUtils list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<SysUserDO> sysUserList = userService.list(query);
		int total = userService.count(query);
		PageUtils pageUtil = new PageUtils(sysUserList, total);
		return pageUtil;
	}

	@RequiresPermissions("sys:user:add")
	@Log("添加用户")
	@GetMapping("/add")
	String add(Model model) {
		List<RoleDO> roles = roleService.list();
		model.addAttribute("roles", roles);
		return "sys/user/add";
	}

	@Log("编辑用户")
	@GetMapping("/edit/{id}")
	String edit(Model model, @PathVariable("id") Long id) {
		SysUserDO userDO = userService.get(id);
		model.addAttribute("user", userDO);
		List<RoleDO> roles = roleService.list(id);
		model.addAttribute("roles", roles);
		return "sys/user/edit";
	}

	@Log("保存用户")
	@PostMapping("/save")
	@ResponseBody
	R save(SysUserDO user) {
		user.setPassword(MD5Utils.encrypt(user.getUsername(), user.getPassword()));
		if (userService.save(user) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@Log("更新用户")
	@PostMapping("/update")
	@ResponseBody
	R update(SysUserDO user) {
		if (userService.update(user) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@Log("删除用户")
	@PostMapping("/remove")
	@ResponseBody
	Map<String, Boolean> remove(Long id) {
		Map<String, Boolean> rMap = new HashMap<>();
		rMap.put("success", userService.remove(id) > 0);
		return rMap;
	}

	@Log("批量删除用户")
	@PostMapping("/batchRemove")
	@ResponseBody
	R batchRemove(@RequestParam("ids[]") Long[] userIds) {

		List<Long> Ids = Arrays.asList(userIds);
		int r = userService.batchremove(Ids);
		System.out.println(r);
		if (r > 0) {
			return R.ok();
		}
		;
		return R.error();
	}

	@PostMapping("/exit")
	@ResponseBody
	boolean exit(@RequestParam("username") String username) {
		return !userService.exit(username);// 存在，不通过，false
	}

	@Log("请求更改用户密码")
	@GetMapping("/resetPasswd")
	String resetPwd(Model model) {
		Subject subject = SecurityUtils.getSubject();
		SysUserDO sysUserDO = (SysUserDO) subject.getPrincipal();
		model.addAttribute("user", sysUserDO);
		return "sys/user/reset_pwd";
	}

	@Log("提交更改用户密码")
	@PostMapping("/resetPwd")
	@ResponseBody
	R resetPwd(@RequestParam Map<String, Object> params) {
		System.out.println(params);
		//获取旧密码
		String oldPasswd = params.get("oldPasswd").toString();
		//获取新密码
		String newPasswd = params.get("newPasswd1").toString();
		//获取用户id
		String userId = params.get("userId").toString();
		SysUserDO userDO = userService.get(Long.valueOf(userId));
		//校验旧密码
		if(!MD5Utils.encrypt(userDO.getUsername(), oldPasswd).equals(userDO.getPassword())){
			return R.error("密码错误");
		}else{
			userDO.setPassword(MD5Utils.encrypt(userDO.getUsername(),newPasswd));
			if(userService.resetPasswd(MD5Utils.encrypt(userDO.getUsername(),newPasswd), Long.valueOf(userId))>0){
				return R.ok();
			}
		}
//		user.setPassword(MD5Utils.encrypt(userService.get(user.getUserId()).getUsername(), user.getPassword()));
//		if (userService.resetPwd(user) > 0) {
//			return R.ok();
//		}
		return R.error();
	}

}
